﻿using GodSystemBLL;
using GodSystemModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GodSystem.FormChildren
{
    public partial class FormUserPower : Form
    {
        private static FormUserPower formUserPower = null;
        private static int id = 0;
        public FormUserPower()
        {
            InitializeComponent();
            InitData();
        }

        private void InitData()
        {
            string userName = FormUserList.usersModel.Name;
            int userId = FormUserList.usersModel.Id;
            id = userId;
            label_UserName.Text = "用户：" + userName + " 权限列表";
            // 加载权限列表
            PowersBLL powersBLL = new PowersBLL();

            // 获取用户已经拥有的权限
            Dictionary<int, int> userPowerId = new Dictionary<int, int>();
            List<UserrolepowerModel> userPower = powersBLL.GetUserPowerByUserId(userId);
            for (int i = 0; i < userPower.Count; i++)
            {
                userPowerId.Add(userPower[i].PowerId, userPower[i].Id);
            }

            // 展示权限列表
            List<PowersModel> powerList = powersBLL.GetPowersList();
            int j = 0;
            int startLocationX = 20;
            int startLocationY = 20;
            int sizeX = 100;
            int sizeY = 20;
            int sep = 0;
            int flag = 1;
            // 记录遍历过的组ID
            List<int> hasCheckGroupId = new List<int>();
            // 保存权限组
            Dictionary<int, string> powerGroup = new Dictionary<int, string>();

            List<PowerGroupModel> pgList = powersBLL.GetPowerGroup();
            for (int i = 0; i < pgList.Count; i++)
            {
                powerGroup.Add(pgList[i].Id, pgList[i].GroupName);
            }

            for (int i = 0; i < powerList.Count; i++)
            {
                //排列指定的数量就换行,并且Y坐标增加
                if (j > 0 && flag % 6 == 0)
                {
                    sep++;
                    j = 0;
                }
                flag++;
                PowersModel power = powerList[i];

                if (!hasCheckGroupId.Contains(power.PowerGroupId))
                {
                    hasCheckGroupId.Add(power.PowerGroupId);
                    Label labelTag = new Label();
                    labelTag.Anchor = AnchorStyles.Left | AnchorStyles.Top;
                    if (powerGroup.ContainsKey(power.PowerGroupId))
                    {
                        labelTag.Text = powerGroup[power.PowerGroupId];
                    }
                    else
                    {
                        powerGroup.Add(power.PowerGroupId, "未分组");
                        labelTag.Text = "未分组";
                    }

                    labelTag.ForeColor = Color.Brown;
                    labelTag.Font = new Font(FontFamily.GenericSansSerif, 16);
                    // 没有换行时，另起一行
                    if (j != 0)
                    {
                        sep++;
                    }
                    int labelLocationY = startLocationY + (startLocationY + sizeY) * sep;
                    labelTag.Location = new Point(20, labelLocationY);
                    labelTag.Size = new Size(sizeX, sizeY + 10);
                    panel_UserPowerList.Controls.Add(labelTag);
                    // 再增一行
                    sep++;
                    j = 0;
                    flag = 1;
                }

                int newstartLocationX = startLocationX + (startLocationX + sizeX) * j;
                int newstartLocationY = startLocationY + (startLocationY + sizeY) * sep;

                CheckBox checkBox = new CheckBox();
                checkBox.Anchor = AnchorStyles.Left | AnchorStyles.Top;
                checkBox.Location = new Point(newstartLocationX, newstartLocationY);
                checkBox.Size = new Size(sizeX, sizeY);
                checkBox.Text = power.Remark;
                checkBox.Tag = power;
                if (userPowerId.ContainsKey(power.Id))
                {
                    checkBox.Checked = true;
                }

                checkBox.Name = "power-" + power.Id;
                j++;
                panel_UserPowerList.Controls.Add(checkBox);
            }
        }



        public static FormUserPower CreateInstance()
        {
            if (formUserPower == null || formUserPower.IsDisposed)
            {
                formUserPower = new FormUserPower();
            }
            return formUserPower;
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void button_PowerUpdate_Click(object sender, EventArgs e)
        {
            List<int> powerIds = new List<int>();
            foreach(Control control in panel_UserPowerList.Controls)
            {
                if(control is CheckBox)
                {
                    CheckBox checkBox = (CheckBox)control;
                    if (checkBox.Checked)
                    {
                        PowersModel powers = (PowersModel)checkBox.Tag;
                        powerIds.Add(powers.Id);
                        
                    }
                }
            }

            UsersBLL usersBLL = new UsersBLL();
            usersBLL.UpdateUserPower(id, powerIds);
            MessageBox.Show("修改成功");
        }
    }
}
